package system

import (
	"faker/project-public/common/response"
	"faker/project-public/global"
	sysResponse "faker/project-public/model/response"
	"faker/project-public/model/sys"
	"faker/project-public/request"
	"faker/project-public/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"time"
)

type SysUserApi struct {
}

func (u *SysUserApi) CreatUser(c *gin.Context) {
	var register request.Register
	err := c.ShouldBindJSON(&register)
	if err != nil {
		response.ErrorWithMessage(err.Error(), c)
		global.FAKER_LOG.Error("绑定失败", zap.Error(err))
		return
	}
	err = utils.Verify(register, utils.RegisterVerify)
	if err != nil {
		response.ErrorWithMessage(err.Error(), c)
		global.FAKER_LOG.Error("验证失败", zap.Error(err))
		return
	}

	user := &sys.SysUser{Username: register.Username, Name: register.Name, PasswordHash: register.Password, RoleId: uint(register.RoleId), Status: register.Status, Mobile: register.Mobile, Email: register.Email, CreateTime: int(time.Now().Unix()), UpdateTime: int(time.Now().Unix())}
	userReturn, err := userService.CreateUser(*user)
	if err != nil {
		global.FAKER_LOG.Error("新增失败!", zap.Error(err))
		response.ErrorWithDetailed(sysResponse.SysUserResponse{User: userReturn}, "新增失败", c)
		return
	}
	response.SuccessWithDetailed(sysResponse.SysUserResponse{User: userReturn}, "新增成功", c)

}

// GetUserInfo 获取用户信息
func (u *SysUserApi) GetUserInfo(c *gin.Context) {
	userInfo := utils.GetUserInfo(c)
	UserInfo, err := userService.GetUserInfo(int(userInfo.ID))
	if err != nil {
		response.ErrorWithMessage(err.Error(), c)
		global.FAKER_LOG.Error("创建失败", zap.Error(err))
		return
	}
	response.SuccessWithDetailed(gin.H{"user_info": UserInfo}, "查询成功", c)

}

// GetListUserInfo 获取列表用户信息
func (u *SysUserApi) GetListUserInfo(c *gin.Context) {
	var idInfo request.GetById
	err := c.ShouldBindJSON(&idInfo)
	if err != nil {
		response.ErrorWithMessage(err.Error(), c)
		global.FAKER_LOG.Error(err.Error(), zap.Error(err))
		return
	}
	err = utils.Verify(idInfo, utils.IdVerify)
	if err != nil {
		response.ErrorWithMessage(err.Error(), c)
		return
	}
	if idInfo.ID == 0 {
		response.SuccessWithMessage("查询成功", c)
	} else {
		UserInfo, err := userService.GetUserInfo(idInfo.ID)
		if err != nil {
			response.ErrorWithMessage(err.Error(), c)
			global.FAKER_LOG.Error("创建失败", zap.Error(err))
			return
		}
		response.SuccessWithDetailed(gin.H{"user_info": UserInfo}, "查询成功", c)
	}

}

// GetUserList 用户列表
func (u *SysUserApi) GetUserList(c *gin.Context) {
	var pageInfo request.UserList
	err := c.ShouldBindJSON(&pageInfo)
	if err != nil {
		response.ErrorWithMessage(err.Error(), c)
		global.FAKER_LOG.Error("参数有误", zap.Error(err))
		return
	}
	err = utils.Verify(pageInfo, utils.PageVerify)
	if err != nil {
		response.ErrorWithMessage(err.Error(), c)
		global.FAKER_LOG.Error("验证参数有误", zap.Error(err))
		return
	}
	list, total, err := userService.GetUserList(pageInfo)
	if err != nil {
		response.ErrorWithMessage(err.Error(), c)
		global.FAKER_LOG.Error("查询失败", zap.Error(err))
		return
	}
	response.SuccessWithDetailed(response.PageResult{
		List:     list,
		Total:    total,
		Page:     pageInfo.Page,
		PageSize: pageInfo.PageSize,
	}, "查询成功", c)
}

// SetUserInfo 修改用户
func (u *SysUserApi) SetUserInfo(c *gin.Context) {
	var setUser request.SetUser
	err := c.ShouldBindJSON(&setUser)
	if err != nil {
		response.ErrorWithMessage(err.Error(), c)
		global.FAKER_LOG.Error("绑定失败", zap.Error(err))
		return
	}
	err = utils.Verify(setUser, utils.SetUserVerify)
	if err != nil {
		response.ErrorWithMessage(err.Error(), c)
		global.FAKER_LOG.Error("验证失败", zap.Error(err))
		return
	}
	user := sys.SysUser{ID: setUser.ID, Username: setUser.Username, Name: setUser.Name, RoleId: uint(setUser.RoleId), Status: setUser.Status, Mobile: setUser.Mobile, Email: setUser.Email, UpdateTime: int(time.Now().Unix())}
	err = userService.UpdateUser(user)
	if err != nil {
		global.FAKER_LOG.Error("更新失败!", zap.Error(err))
		response.ErrorWithMessage("更新失败", c)
		return
	}
	response.SuccessWithMessage("更新成功", c)

}

func (u *SysUserApi) DeleteUser(c *gin.Context) {
	var idInfo request.GetById
	err := c.ShouldBindJSON(&idInfo)
	if err != nil {
		response.ErrorWithMessage(err.Error(), c)
		global.FAKER_LOG.Error(err.Error(), zap.Error(err))
		return
	}
	err = utils.Verify(idInfo, utils.IdVerify)
	if err != nil {
		response.ErrorWithMessage(err.Error(), c)
		return
	}
	err = userService.DeleteUser(idInfo.ID)
	if err != nil {
		response.ErrorWithMessage(err.Error(), c)
		global.FAKER_LOG.Error("删除失败", zap.Error(err))
		return
	}
	response.SuccessWithMessage("删除成功", c)
}

// ChangePassword 修改密码
func (u *SysUserApi) ChangePassword(c *gin.Context) {
	var upPass request.UpdatePass
	err := c.ShouldBindJSON(&upPass)
	if err != nil {
		response.ErrorWithMessage(err.Error(), c)
		return
	}
	userInfo := utils.GetUserInfo(c)
	err = utils.Verify(upPass, utils.RepassVerify)
	if err != nil {
		response.ErrorWithMessage(err.Error(), c)
		global.FAKER_LOG.Error("验证失败", zap.Error(err))
		return
	}
	user := &sys.SysUser{ID: userInfo.ID, PasswordHash: upPass.OldPassword}
	_, err = userService.ChangePassword(user, upPass.PasswordHash, upPass.RePassword)
	if err != nil {
		global.FAKER_LOG.Error("修改失败!", zap.Error(err))
		response.ErrorWithMessage("修改失败，原密码与当前账户不符", c)
		return
	}
	response.SuccessWithMessage("修改成功", c)

}

// GetMenusInfos 获取用户菜单和权限
func (u *SysUserApi) GetMenusInfos(c *gin.Context) {
	roleId := utils.GetUserRoleId(c)
	userMenus, _ := ruleService.GetUserMenuTree(int(roleId))
	response.SuccessWithDetailed(gin.H{"user_menus": userMenus}, "查询成功", c)
}
